<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/20
 * Time: 下午5:44
 */

class ModRetain {

    public static function indexData($params){
        return self::indexDataByKPI($params);
    }
    
    private static function indexDataByKPI($params){
    	$where  = ' WHERE RecDate BETWEEN :begin AND :end 
    			        AND Period=:period
                        AND GameSrc=:game ';
    
    	$args   = array(
    			':begin'    => $params['time_start'],
    			':end'      => $params['time_end'],
    			':period'   => 'Daily',
    			':game'     => $params['g'],
    	);
    	
    	$user = AccAuth::me();
    	if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
    		$where .= " AND Platform=:p ";
    		$args[':p'] = $params['os'];
    	}
    	
    	if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
    		$modServer = new ModServer();
    		$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
    		$where .= " AND ( ";
    		foreach ($list as $key=>$val){
    			$where .= " SID = '{$val['develop_id']}'  or";
    		}
    		$where = substr($where, 0, -2);
    		$where .= " ) ";
    	}else{
    		$where .= " AND SID=:ser ";
    		$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
    	}
    
    	$table = 'tblGameRole';
    
    	$sql = "SELECT * FROM {$table} {$where}";

    	$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
    	return $dbLink->query($sql, $args);
    }

    public static function SourceData($params){
        $table = 'tblGameSourceRemain';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end
                            AND Platform=:platform AND GameSrc=:game ';
        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => $params['period'],
            ':platform' => $params['p'],
            ':game'     => $params['g'],
        );
        if($params["kpi"] == "NewAndSec"){
            $sql = "SELECT RecDate,SID,KPI,SUM(KPIValue) AS KPIValue,SUM(RemainDay1) AS RemainDay1,SUM(RemainDay7) AS RemainDay7,SUM(RemainDay14) AS RemainDay14,
            SUM(RemainDay21) AS RemainDay21 FROM {$table} {$where} and KPI in('NewAdRemain','SecAdRemain') group by RecDate,Platform,GameSrc,SID";
        }else{
            $sql = "SELECT RecDate,SID,KPI,KPIValue,RemainDay1,RemainDay7,RemainDay14,RemainDay21 FROM {$table} {$where} AND KPI='{$params['kpi']}'";
        }
        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }

    public static function channelData($params){
        $table = 'tblChannelRemain';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period=:period
                        AND Platform=:platform AND GameSrc=:game AND SID=:server AND KPI = :kpi ';

        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => 'Daily',
            ':platform' => $params['os'],
            ':game'     => $params['g'],
            ':server'   => empty($params['server']) ? 'ALL' : $params['server'],
            ':kpi'      => $params['kpi'],
        );

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }

    public static function roleData($params){
        $table = 'tblGameRoleRocRemain';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period=:period
                        AND Platform=:platform AND GameSrc=:game AND SID=:server ';

        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => 'Daily',
            ':platform' => $params['os'],
            ':game'     => $params['g'],
            ':server'   => empty($params['server']) ? 'ALL' : $params['server'],
        );

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }
} 